package april.test_4_21;

import java.util.ArrayList;

public class Solution {
    boolean flag = false;
    int t;
    public boolean kInArray (int k, int target) {
        // write code here
        t = target;
        if(k < 0 && t < 0) {
            dfs2(k);
        } else {
            dfs(k);
        }
        if(flag) {
            return true;
        }
        return false;
    }
    public void dfs(int x) {
        if(x > t || flag) return;
        if(x == t) {
            flag = true;
            return;
        }
        if((t - 1) %2 == 0) dfs(x*2 + 1);
        if((t - 1) % 3 == 0) dfs(x * 3 +1);
        return;
    }
    public void dfs2(int x) {
        if(x < t || flag ) return;
        if( x == t) {
            flag = true;
            return;
        }
        dfs2(x * 2 + 1);
        dfs2(x * 3 + 1);
        return;
    }
    public int[] flipImage (int width, int[] pixels) {
        // write code here
        int[] arr = new int[width*4 + 1];
        ArrayList<Integer> arrayList = new ArrayList<>();
        for (int i = 0; i < pixels.length; i++) {
            arr[i %(width * 4)] = pixels[i];
            if((i + 1) % (width * 4) == 0) {
                for (int j = width; j >= 1; j--) {
                    for (int k = (j - 1) * 4; k <= (j - 1)*4 + 3; k++) {
                        arrayList.add(arr[k]);
                    }
                }
            }
        }
        int[] results = new int[arrayList.size()];
        for (int i = 0; i < arrayList.size(); i++) {
            results[i] = arrayList.get(i);
        }
        return results;
    }
}
